package com.ndp.fb.rdb.model;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.enums.publish.BillingEvent;
import com.ndp.fb.enums.publish.OptimizationGoal;
import com.ndp.fb.rdb.annotation.Column;
import com.ndp.fb.rdb.annotation.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Sisi on 15-2-26.
 */
@Setter
@Getter
@NoArgsConstructor
public class AdSet implements Serializable {
    /**
     * 描述   (@author: giser.yang)
     */
    private static final long serialVersionUID = -3178277392923892672L;
    @Id
    /**
     * id
     */
    private Long id;
    /**
     * ad_set_id
     */
    private Long adSetId;
    /**
     * fb_account_id
     */
    private Long fbAccountId;
    /**
     * campaign_group_id
     */
    private Long campaignGroupId;
    /**
     * publish_batch_id
     */
    private Long publishBatchId;
    /**
     * fb_connection_object_id
     */
    private Long fbConnectionObjectId;
    /**
     * product_conf_id
     */
    private Long productConfId;
    /**
     * name
     */
    private String name;
    /**
     * campaign_status
     */
    private String campaignStatus;
    /**
     * daily_budget
     */
    private Integer dailyBudget;
    /**
     * lifetime_budget
     */
    private Integer lifetimeBudget;
    /**
     * start_time
     */
    private Date startTime;
    /**
     * end_time
     */
    private Date endTime;
    /**
     * start_utc_time
     */
    private Long startUtcTime;
    /**
     * end_utc_time
     */
    private Long endUtcTime;
    /**
     * updated_time
     */
    private Date updatedTime;
    /**
     * created_time
     */
    private Date createdTime;
    /**
     * created_utc_time
     */
    private Long createdUtcTime;
    /**
     * updated_utc_time
     */
    private Long updatedUtcTime;
    /**
     * bid_type
     */
    private String bidType;
    /**
     * bid_info
     */
    private String bidInfo;
    /**
     * promoted_object
     */
    private String promotedObject;
    /**
     * name_status
     */
    private Integer nameStatus;
    /**
     * open_status
     */
    private Integer openStatus;
    /**
     * price_status
     */
    private Integer priceStatus;
    /**
     * budget_status
     */
    private Integer budgetStatus;
    /**
     * targeting
     */
    private String targeting;
    @Column("adlabels")
    private String adLabels;
    /**
     * frequency_cap
     */
    private Integer frequencyCap;
    /**
     * frequency_cap_reset_period
     */
    private Integer frequencyCapResetPeriod;
    /**
     * lifetime_frequency_cap
     */
    private Integer lifetimeFrequencyCap;
    /**
     * lifetime_imps
     */
    private Integer lifetimeImps;
    /**
     * optimization_goal
     */
    private String optimizationGoal;
    /**
     * product_ad_behavior
     */
   // private String productAdBehavior;
    /**
     * rf_prediction_id
     */
    private Integer rfPredictionId;
    /**
     * rtb_flag
     */
    private Integer rtbFlag;
    /**
     * pacing_type
     */
    private String pacingType;
    /**
     * budget_remaining
     */
    private Integer budgetRemaining;
    /**
     * bid_amount
     */
    private Integer bidAmount;
    /**
     * billing_event
     */
    private String billingEvent;
    /**
     * buying_type
     */
    private String buyingType;
    /**
     * effective_status
     */
    private String effectiveStatus;
    /**
     * is_autobid
     */
    private Boolean isAutobid;

    private Long ecTemplateMaterielId;

    private String productSetMongoId;

    public void setAutobid(Boolean autobid) {
        isAutobid = autobid;
    }
    public void setIsAutobid(Boolean autobid) {
        isAutobid = autobid;
    }

    public String getBidType() {
        if (bidType == null) { //2.4
            if (OptimizationGoal.IMPRESSIONS.getCode().equals(getOptimizationGoal())
                    && BillingEvent.IMPRESSIONS.getCode().equals(getBillingEvent())) {
                bidType = "CPM";
            } else if (OptimizationGoal.LINK_CLICKS.getCode().equals(getOptimizationGoal())
                    && BillingEvent.LINK_CLICKS.getCode().equals(getBillingEvent())) {
                bidType = "CPC";
            } else if (OptimizationGoal.APP_INSTALLS.getCode().equals(getOptimizationGoal())
                    && BillingEvent.APP_INSTALLS.getCode().equals(getBillingEvent())) {
                bidType = "CPA";
            } else if (BillingEvent.IMPRESSIONS.getCode().equals(getBillingEvent())
                    && !OptimizationGoal.IMPRESSIONS.getCode().equals(getOptimizationGoal())) {
                bidType = "ABSOLUTE_OCPM";
            }
        }
        return bidType;
    }

    public String getBidInfo() {
        if (bidInfo == null) {
            Map<String, Integer> info = new HashMap<>();
            info.put(OptimizationGoal.toOldVersion(getOptimizationGoal()), getBidAmount());
            return JSON.toJSONString(info);
        }
        return bidInfo;
    }

    @Override
    public int hashCode() {
        return adSetId.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof AdSet) {
            if (this.adSetId != null && this.adSetId.equals(((AdSet) obj).getAdSetId())) {
                if (this.updatedUtcTime != null && this.updatedUtcTime.equals(((AdSet) obj).getUpdatedUtcTime())) {
                    if (this.campaignStatus != null && this.campaignStatus.equals(((AdSet) obj).getCampaignStatus())) {
                        if (this.effectiveStatus != null && this.effectiveStatus.equals(((AdSet) obj).getEffectiveStatus())) {
                            return true;
                        }
                    }
                }
            }

        }
        return false;
    }

}
